/**
 * Filename: DetailAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/15 10:08
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.HistoryCoverTheme;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.YNUrlHelper;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class HistoryCoverThemeAdapter extends
        RecyclerView.Adapter<HistoryCoverThemeAdapter.ViewHolder> {
    private Context context;
    private ImageLoader mImageLoader;
    private LayoutInflater mInflater;
    private List<HistoryCoverTheme> mDatas;

    public HistoryCoverThemeAdapter(Context context, List<HistoryCoverTheme> datats) {
        this.context = context;
        mImageLoader = ImageLoader.getInstance();
        mInflater = LayoutInflater.from(context);
        mDatas = datats;
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        SimpleDraweeView mImg;
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.yn_item_covertheme_detail,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);

        viewHolder.mImg = (SimpleDraweeView) view
                .findViewById(R.id.id_dateil_item_image);
        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
        String path = YNUrlHelper.getStatusUrl(context, mDatas.get(i).getHistoryCoverPicture());
        Uri uri = Uri.parse(path);
        ImageOptHelper.showFrescoImg(uri, viewHolder.mImg, context);
        GenericDraweeHierarchy hierarchy = viewHolder.mImg.getHierarchy();
        hierarchy.setPlaceholderImage(R.drawable.covertheme_empty);
        if (mOnItemClickLitener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickLitener.onItemClick(viewHolder.itemView, i);
                }
            });
        }
        viewHolder.setIsRecyclable(false);
    }

}
